package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;

    // 1. 根据用户名来查找用户信息.
    public User selectByName(String username) {
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from user2 where username = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setArticleNum(resultSet.getInt("articleNum"));
                user.setLikeNum(resultSet.getInt("likeNum"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    // 2. 根据用户 id 来找用户信息.
    public User selectById(int userId){
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from user2 where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setArticleNum(resultSet.getInt("articleNum"));
                user.setLikeNum(resultSet.getInt("likeNum"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    public void updateMinus(int userId){
        try {
            connection = DBUtil.getConnection();
            String sql = "update user2 set articleNum = articleNum - 1 where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            statement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
    }

    public void updatePlus(int userId){
        try {
            connection = DBUtil.getConnection();
            String sql = "update user2 set articleNum = articleNum + 1 where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            statement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
    }
}
